package com.library.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.library.entity.Admin;
import com.library.entity.Reader;
import com.library.service.IAdminService;
import com.library.service.IReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author weiwu
 * @description 演示用的控制器
 * @date 2018/10/1 9:32
 */
@Controller
public class LoginController {

    /*
     * 因为登录的时候，管理员和用户都是同一个登录入口，所以此处需要注入到两个service对象
     * */
    @Autowired
    IReaderService readerService;
    @Autowired
    IAdminService adminService;

    /**
     * 跳转到登录页面
     *
     * @return
     */
    @RequestMapping ( {"/index", "/"} )
    public String index() {
        return "login";
    }

    /**
     * 处理登录请求
     * 所有用户（管理员和读者）都是同一个入口
     *
     * @param readerId 用户的校园卡卡号
     * @param password 用户的密码
     * @param request
     * @return
     */
    @RequestMapping ( "/login" )
    public String doLogin( @RequestParam ( "readerId" ) String readerId,
                           @RequestParam ( "password" ) String password,
                           HttpServletRequest request ) {

        Reader reader = readerService.selectOne(new EntityWrapper<Reader>()
                                                    .eq("readerId", readerId)
                                                    .eq("password", password));
        Admin admin = adminService.selectOne(new EntityWrapper<Admin>()
                                                 .eq("adminId", readerId)
                                                 .eq("password", password));

        /*判断当前用户是管理员还是普通读者的标志*/
        boolean isAdmin = false;
        boolean isReader = false;

        if ( admin != null ) {
            isAdmin = true;
        }
        if ( reader != null ) {
            isReader = true;
        }

        HttpSession session = request.getSession();

        /*
         * 根据登录的用户名，跳转到不同的页面
         * */
        if ( isAdmin == true ) {
            //跳转到管理员的页面
            session.setAttribute("admin", admin);
            return "admin/admin";
        } else if ( isReader == true ) {
            //跳转到读者的界面
            session.setAttribute("reader", reader);
            return "reader/reader";
        } else {
            //跳转到游客界面
            return "visitor/visitor";
        }

    }

    @GetMapping ( "/visit" )
    public String doVisit() {
        //跳转到游客界面
        return "visitor/visitor";
    }

    /**
     * 用户退出登录
     *
     * @param request
     * @return
     */
    @GetMapping ( "/logout" )
    public String doLogout( HttpServletRequest request ) {
        request.getSession().invalidate();
        return "login";
    }
}
